﻿// ===============================================================================
// XCI.Parent 西安交通信息WinForm开发框架 2019 版权所有
// ===============================================================================

using System;
using System.Windows.Forms;
using XCI.Sys.Model;
using XCI.Sys.Service;
using XCI.Windows.Helper;

namespace XCI.Sys.Forms
{
    public partial class FrmDeptEdit : FrmSysBase
    {
        private bool created;
        private SysDept model;
        private SysDept pmodel;

        public FrmDeptEdit()
        {
            InitializeComponent();
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keys)
        {
            return OnEsc(msg, keys, () => { EditFormEscResultCancel(); });
        }

        /// <summary>
        /// 页面初始化
        /// </summary>
        protected void InitForm()
        {
            BindModel();
        }

        /// <summary>
        /// 设置数据对象
        /// </summary>
        /// <param name="p_created">是否新建</param>
        /// <param name="p_model">数据对象</param>
        /// <param name="p_parentModel">父节点对象</param>
        public void SetModel(bool p_created, SysDept p_model, SysDept p_parentModel)
        {
            created = p_created;
            model = p_model;
            pmodel = p_parentModel;
            Text = created ? "新建系统部门" : "修改系统部门";
            ckContinue.Visible = ckReserve.Visible = p_created;
        }

        /// <summary>
        /// 绑定数据到界面
        /// </summary>
        private void BindModel()
        {
            BindParentModel(model, pmodel, labParentName, layParentClean);
            WinHelper.SetFormData(layoutPanel, model);
            editName.Select();
        }

        /// <summary>
        /// 保存数据对象
        /// </summary>
        private bool SaveModel()
        {
            WinHelper.GetFormData(layoutPanel, model);

            var isValid = ValidModel(model);
            if (!isValid) return false;

            if (created)
            {
                return DeptService.Instance.Insert(model);
            }

            return DeptService.Instance.Update(model);
        }

        /// <summary>
        /// 页面加载事件
        /// </summary>
        private void FrmDeptEdit_Load(object sender, EventArgs e)
        {
            InitForm();
        }

        private void CkContinue_CheckedChanged(object sender, EventArgs e)
        {
            ckReserve.Enabled = ckContinue.Checked;
        }

        /// <summary>
        /// 清除父节点
        /// </summary>
        private void btnParentClean_Click(object sender, EventArgs e)
        {
            pmodel = null;
            BindParentModel(model, pmodel, labParentName, layParentClean);
        }

        /// <summary>
        /// 保存事件
        /// </summary>
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!SaveModel()) return;
            if (ckContinue.Checked) //连续保存
            {
                MessageBoxHelper.ShowMessage("保存成功");
                var nmodel = ckReserve.Checked ? DeptService.Instance.Copy(model) : DeptService.Instance.New();
                SetModel(true, nmodel, pmodel);
                BindModel();
            }
            else
            {
                ResultOk();
            }
        }

        /// <summary>
        /// 关闭事件
        /// </summary>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            //如果点关闭按钮后想刷新表格就用ResultOk,不刷新用ResultCancel
            ResultOk();
            //ResultCancel();
        }
    }
}